package com.cole.miniapp.project.controller;

import com.cole.miniapp.common.annotation.LoginUser;
import com.cole.miniapp.common.bean.ResponseResult;
import com.cole.miniapp.project.annotation.LoginStore;
import com.cole.miniapp.project.domain.*;
import com.cole.miniapp.project.domain.vo.FcmBusinessVO;
import com.cole.miniapp.project.service.PayService;
import com.cole.miniapp.project.service.StoreService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Create tao on 2019/5/17
 */
@RestController
@RequestMapping("/api/store")
public class StoreController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private StoreService storeService;

    @Autowired
    private PayService payService;

    /**
     * 商家入口
     *
     * @param fcmUser
     * @param page
     * @return
     */

    @PostMapping(value = "/storeEntrance")
    public ResponseResult storeEntrance(@LoginUser FcmUser fcmUser,
                                        @RequestParam(value = "page", defaultValue = "0", required = false) int page) {

        ResponseResult result = ResponseResult.success();
        List<FcmStore> list = storeService.storeEntrance(fcmUser.getId(), page);
        if (list == null) {
            result = ResponseResult.error();
            result.setMsg("没有店铺");
        }
        result.setData(list);
        return result;
    }

    /**
     * 老板获取积分赠送比例
     *
     * @param fcmBusiness
     * @return
     */
    @PostMapping(value = "/getFractionProportion")
    public ResponseResult getFractionProportion(@LoginStore FcmBusiness fcmBusiness) {
        ResponseResult result = ResponseResult.error();
        if (fcmBusiness.getType() != 0) {
            result.setMsg("你没有权限");
        }
        //判断参数参数
        BigDecimal fractionProportion= storeService.getFractionProportion(fcmBusiness);
        result = ResponseResult.success();
        result.setData(fractionProportion);
        result.setMsg("获取成功");
        return result;
}


    /**
     * 老板设置积分赠送比例
     *
     * @param fcmBusiness
     * @param proportion
     * @return
     */
    @PostMapping(value = "/setFractionProportion")
    public ResponseResult setFractionProportion(@LoginStore FcmBusiness fcmBusiness,
                                                @RequestParam(value = "proportion", defaultValue = "0.01", required = false) BigDecimal proportion) {
        ResponseResult result = ResponseResult.error();
        if (fcmBusiness.getType() != 0) {
            result.setMsg("你没有权限");
        }
        //判断参数参数
        proportion = proportion.setScale(2, BigDecimal.ROUND_DOWN);
        BigDecimal zero = new BigDecimal(0).setScale(2, BigDecimal.ROUND_DOWN);
        BigDecimal one = new BigDecimal(1).setScale(2, BigDecimal.ROUND_DOWN);
        int i = zero.compareTo(proportion);
        int j = one.compareTo(proportion);
        if (i == -1 && j == 1) {
            result = ResponseResult.success();
            boolean isExit = storeService.setFractionProportion(fcmBusiness, proportion);
            if (isExit) {
                result.setMsg("设置成功");
                return result;
            } else {
                result.setMsg("修改成功");
                return result;
            }
        }
        result.setMsg("传入参数不合法");
        return result;
    }


    /**
     * 店铺帮用户充值
     *
     * @param fcmBusiness
     * @param money
     * @return
     */
    @PostMapping(value = "/storeRechargesToUser")

    public ResponseResult storeRechargesToUser(@LoginStore FcmBusiness fcmBusiness,
                                               @RequestParam(value = "userId") Long userId,
                                               @RequestParam(value = "phone") String phone,
                                               @RequestParam(value = "money") BigDecimal money) {
        ResponseResult result = ResponseResult.error();
        if (fcmBusiness.getType().toString().equals("0")) {
            Integer state = payService.storeRechargesToUser(fcmBusiness, userId, phone, money);
            if (state == 0) {
                result.setMsg("用户不存在");
            } else if (state == 1) {
                result.setMsg("手机号输入有误");
            } else if (state == 2) {
                result.setMsg("店铺余额不足，无法充值");
            } else {
                result = ResponseResult.success();
                result.setMsg("充值成功");
            }
            return result;
        }
        result.setMsg("没有设置权限");
        return result;
    }

    /**
     * 添加员工
     *
     * @param fcmBusiness
     * @param phone
     * @param name
     * @param type
     * @return
     */
    @PostMapping(value = "/addStaff")
    public ResponseResult addStaff(@LoginStore FcmBusiness fcmBusiness,
                                   @RequestParam(value = "storeUserId") Long storeUserId,
                                   @RequestParam(value = "phone") String phone,
                                   @RequestParam(value = "name") String name,
                                   @RequestParam(value = "type") Byte type
    ) {
        ResponseResult result = ResponseResult.error();
        if (fcmBusiness.getType() == 2) {
            result.setMsg("你没有权限");
        }
        Integer isOk = storeService.addStaff(fcmBusiness, storeUserId, name, phone, type);
        if (isOk == 1) {
            result.setMsg("没有此用户");
        } else if (isOk == 2) {
            result.setMsg("手机号码不正确");
        } else if (isOk == 3) {
            result = ResponseResult.success();
            result.setMsg("设置成功");
        } else if (isOk == 4) {
            result.setMsg("员工已存在");
        } else {
            result.setMsg("你没有权限");
        }
        return result;

    }

    /**
     * 更新门店成员身份，目前只能老板设置
     *
     * @param fcmBusiness
     * @param storeUserId
     * @return
     */
    @PostMapping(value = "/setStoreIdentity")
    public ResponseResult setStoreIdentity(@LoginStore FcmBusiness fcmBusiness,
                                           @RequestParam(value = "storeUserId") Long storeUserId,
                                           @RequestParam(value = "type") Byte type) {
        ResponseResult result = ResponseResult.error();
        if (fcmBusiness.getType() != 0) {
            result.setMsg("你没有权限");
        }
        Integer isOk = storeService.setStoreIdentity(fcmBusiness, storeUserId, type);
        if (isOk == 1) {
            result.setMsg("你没有权限");
        } else if (isOk == 2) {
            result.setMsg("不存在该店员");
        }
        result = ResponseResult.success();
        result.setMsg("更新成功");
        return result;
    }


    /**
     * 删除店员
     *
     * @param fcmBusiness
     * @param storeUserId
     * @return
     */
    @PostMapping(value = "/deleteStaff")
    public ResponseResult deleteStaff(@LoginStore FcmBusiness fcmBusiness,
                                      @RequestParam(value = "storeUserId") Long storeUserId) {
        ResponseResult result = ResponseResult.error();
        if (fcmBusiness.getType() == 2) {
            result.setMsg("你没有权限");
        }
        Integer isOk = storeService.deleteStaff(fcmBusiness, storeUserId);
        if (isOk == 1) {
            result.setMsg("你无法删除店长");
        }
        result = ResponseResult.success();
        result.setMsg("删除成功");
        return result;
    }


    /**
     * 获取员工列表
     *
     * @param fcmBusiness
     * @param page
     * @return
     */
    @PostMapping(value = "/getStoreUserList")
    public ResponseResult getStoreUserList(@LoginStore FcmBusiness fcmBusiness,
                                           @RequestParam(value = "page", defaultValue = "0", required = false) int page) {
        ResponseResult result = ResponseResult.success();
        if (fcmBusiness.getType() == 2) {
            result.setMsg("你没有权限");
        }
        List<FcmBusinessVO> list = storeService.getStoreUserList(fcmBusiness, page);
        result.setData(list);
        return result;
    }


    /**
     * 获取员工信息
     *
     * @param fcmBusiness
     * @param storeUserId
     * @return
     */
    @PostMapping(value = "/getStoreUserInfo")
    public ResponseResult getStoreUserInfo(@LoginStore FcmBusiness fcmBusiness,
                                           @RequestParam(value = "storeUserId") Long storeUserId) {
        ResponseResult result = ResponseResult.error();
        if (fcmBusiness.getType() == 2) {
            result.setMsg("你没有权限");
        }
        FcmBusinessVO fcmBusinessVO = storeService.getStoreUserInfo(fcmBusiness, storeUserId);
        if (fcmBusinessVO == null) {
            result.setMsg("无权限操作");
            return result;
        }
        result = ResponseResult.success();
        result.setData(fcmBusinessVO);
        result.setMsg("获取成功");
        return result;
    }


    /**
     * 设置店铺是否营业
     *
     * @param fcmBusiness
     * @return
     */
    @PostMapping(value = "/setStoreOpenOrClose")
    public ResponseResult setStoreOpenOrClose(@LoginStore FcmBusiness fcmBusiness) {
        ResponseResult result = ResponseResult.error();
        if (fcmBusiness.getType() != 0) {
            result.setMsg("你没有权限");
        }
        boolean isOpen = storeService.setStoreOpenOrClose(fcmBusiness);
        result = ResponseResult.success();
        if (isOpen) {
            result.setData(true);
            result.setMsg("开店营业");
            return result;
        }
        result.setData(false);
        result.setMsg("关闭营业");
        return result;
    }


    /**
     * 获取店铺信息
     *
     * @param fcmBusiness
     * @return
     */
    @PostMapping(value = "/getStoreInfo")
    public ResponseResult getStoreInfo(@LoginStore FcmBusiness fcmBusiness) {
        ResponseResult result = ResponseResult.success();
        FcmStore fcmStore = storeService.getStoreInfo(fcmBusiness);
        List<Object> list = new ArrayList<>();
        list.add(fcmStore);
        list.add(fcmBusiness);
        result.setData(list);
        return result;
    }

    /**
     * 赠送积分记录
     *
     * @param fcmBusiness
     * @param page
     * @return
     */
    @PostMapping(value = "/getFractionList")
    public ResponseResult getFractionList(@LoginStore FcmBusiness fcmBusiness,
                                          @RequestParam(value = "page", defaultValue = "0", required = false) int page) {
        ResponseResult result = ResponseResult.success();
        List<FcmUserFraction> list = storeService.getFractionList(fcmBusiness, page);
        result.setData(list);
        return result;
    }


    /**
     * 查询消费记录
     *
     * @param fcmBusiness
     * @param page
     * @return
     */
    @PostMapping(value = "/getConsumpitionList")
    public ResponseResult getConsumpitionList(@LoginStore FcmBusiness fcmBusiness,
                                              @RequestParam(value = "page", defaultValue = "0", required = false) int page) {
        ResponseResult result = ResponseResult.success();
        List<FcmUserConsumption> list = storeService.getConsumpitionList(fcmBusiness, page);
        result.setData(list);
        return result;
    }

    /**
     * 门店充值记录
     *
     * @param fcmBusiness
     * @param page
     * @return
     */
    @PostMapping(value = "/getStoreRechargesList")
    public ResponseResult getStoreRechargesList(@LoginStore FcmBusiness fcmBusiness,
                                                @RequestParam(value = "page", defaultValue = "0", required = false) int page) {
        ResponseResult result = ResponseResult.success();
        List<FcmUserRecharges> list = storeService.getStoreRechargesList(fcmBusiness, page);
        result.setData(list);
        return result;
    }


}